package 力扣;


import java.util.Stack;

/**
 * @author yyq
 * @create 2022-03-30 9:22
 */
public class leetcode20 {
    public static void main(String[] args) {
        isValid("[");
    }
    public static boolean isValid(String s) {

        Stack<Character> stack=new Stack<Character>();
        for (int i = 0; i < s.length(); i++) {
            if(stack.size()==0) {
                stack.push(s.charAt(i));
                continue;
            }
            if(s.charAt(i)==']'){

                Character top = stack.peek();
                if(top=='[') {
                    stack.pop();
                }else {
                    stack.push(s.charAt(i));
                }

            }
            else if(s.charAt(i)=='}'){
                Character top = stack.peek();
                if(top=='{') {
                    stack.pop();
                }
                else {
                    stack.push(s.charAt(i));
                }
            }
            else if(s.charAt(i)==')'){
                Character top = stack.peek();
                if(top=='(') {
                    stack.pop();
                }else {
                    stack.push(s.charAt(i));
                }
            }
            else stack.push(s.charAt(i));
        }
        if(stack.size()==0) return true;
        else return false;

    }

}
